using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinFormsApp1
{
    public partial class Form1 : Form
    {
        OrderService orderService;
        public Form1(OrderService orderService)
        {
            InitializeComponent();
            this.orderService = orderService;
        }

        private void addOrder_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            form2.ShowDialog();
            int orderId = form2.orderId;
            orderService.AddOrder(form2.Order);
            FormSuccess formS_1 = new FormSuccess();
            formS_1.ShowDialog();
        }

        private void deleteOrder_Click(object sender, EventArgs e)
        {
            FormDelete form3 = new FormDelete();
            form3.ShowDialog();
            int OrderId = form3.deleteOrderId;
            orderService.DeleteOrder(OrderId);
        }

        private void searchOrder_Click(object sender, EventArgs e)
        {
            this.textBox1.Clear();
            FormSearch formSearch = new FormSearch(orderService);
            formSearch.ShowDialog();

            List<Order> orders = formSearch.orders;
            foreach (Order o in orders)
            {
                this.textBox1.AppendText(o.ToString());
            }
        }

        private void modifyOrder_Click(object sender, EventArgs e)
        {
            FormModify formModify = new FormModify();
            formModify.ShowDialog();

            foreach (Order order in orderService.orderList.ToArray())
            {
                if (order.id == formModify.orderId)
                {
                    orderService.orderList[orderService.orderList.IndexOf(order)] = formModify.Order;
                }
            }
        }

        private void export_Click(object sender, EventArgs e)
        {
            orderService.Export();
            FormSuccess formSuccess = new FormSuccess();
            formSuccess.ShowDialog();
        }

        private void import_Click(object sender, EventArgs e)
        {
            orderService.Import();
            FormSuccess formSuccess = new FormSuccess();
            formSuccess.ShowDialog();
        }
    }
}